package org.mymybatis.session;

import org.mymybatis.mapping.MappedStatement;
import org.mymybatis.transaction.Transaction;

import java.util.Map;

/**
 * @Auther: sh
 * @Version 1.0
 * @Description: SqlSession工厂对象，使用SqlSessionFactory可以获取会话对象
 */
public class SqlSessionFactory {
    /**
     * 事务管理器属性  例如JDBC
     * 这里要灵活切换，所以应该有一个事务管理器接口
     */
    private Transaction transaction;

    /**
     * 存放sql语句的集合
     * key：namespace+id
     * value：标签对象MappedStatement
     */
    private Map<String, MappedStatement> mappedStatements;


    public SqlSession openSession() {
        transaction.openConnection();
        return new SqlSession(transaction, mappedStatements);
    }

    public SqlSessionFactory(Transaction transaction, Map<String, MappedStatement> mappedStatements) {
        this.transaction = transaction;
        this.mappedStatements = mappedStatements;
    }

    public SqlSessionFactory() {
    }

    public Transaction getTransaction() {
        return transaction;
    }

    public void setTransaction(Transaction transaction) {
        this.transaction = transaction;
    }

    public Map<String, MappedStatement> getMappedStatements() {
        return mappedStatements;
    }

    public void setMappedStatements(Map<String, MappedStatement> mappedStatements) {
        this.mappedStatements = mappedStatements;
    }
}

